'''urllib_post请求百度翻译'''

import urllib.request
import urllib.parse
import json

# # 请求路径
# url = 'https://fanyi.baidu.com/langdetect'
# # 请求头
# headers = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
# }
# # 请求参数
# data = {
#     "query": "remain"
#
# }
# # post请求参数必须编码
# data = urllib.parse.urlencode(data).encode('utf-8')
# request = urllib.request.Request(url=url, data=data, headers=headers)
# response = urllib.request.urlopen(request)
# content = response.read().decode('utf-8')
# print(content)


'''请求路径 百度返回搜索单词接口
    注意：请求头的变动
'''
url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
# 请求头
headers = {
    # 'Accept': '*/*',
    # # 'Accept-Encoding': 'gzip, deflate, br',
    # 'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Acs-Token': '1681184672843_1681184681030_j9lxwb7n3dPVEghmCkuWJLcnRTv8fEMCLYdLzi+FEk+/6xJ3C7+6RJorQoZo3J8nKCTabzBQlMYH7WbSW5eRXNgbbhAUqNO++gcXBMebTDTb4YChGsRKEuVhyW3m4WImc9M6YrMIGElpy0GNV9y1/cUVG4g3WJ87B0FhXY7EY1lG5ax3z/pkMmFIAZD+P9nW2/IYe19j/h+7wGPiDsDRszRfZBIFy8l5VxwDoIfaMYPiK1dRTmHQbFi8nsYfwbaN9z0D7e+L1z0IwDVAcK3Il4pV1U4LCu2kc3V4kfuCAqmUBlTbLOxfsViJ/1i5xZ0zNRku6xVvovx7VKKlCOHn0GDyWzCvxxN6uzwGwGEXriHdUdICjaU+VQw8In8hg8ifnWN5r3Goc2Zo1cVmTeP8ehP1da34TZHN/rvyPJmPnWGwsF9lZYwpYlOZ/Qp/69EsHgAGBked6FEkpUD5jByha9pZtKus6PhtBadd0YyeZ4w=',
    # 'Connection': 'keep-alive',
    # 'Content-Length': '119',
    # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    # 'Cookie': 'BAIDUID=D74FBCCC17F5059BD6537E66151F0551:FG=1; BIDUPSID=ACEB829C291C2FF19CF5888C84F9C1C4; PSTM=1679295369; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; H_PS_PSSID=; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1679295607,1680138269,1680241885; BDSFRCVID=Nj0OJeC627q5i85fakWNbi5TNQ50vY7TH6f3PBvC-x7WClY2gL7PEG0PeM8g0Kub-FL1ogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=JnIHoCPhtDL3DnDkb-r_bn0_hM4X5-RLfbPqah7F5l8-hR6dMprvM450QPbA5tFtWRvts4QRH4QxOKQphTODLtuI5xkt-45EKe3BoRrN3KJmVMK9bT3v5fuQXbOn2-biW2r-2MbdaDJP_IoG2Mn8M4bb3qOpBtQmJeTxoUJ25DnJhbLGe6-bejJbjaufq-jKaK3tsD8KtTrjDCv55MQcy4LdjG5zXnOU35rh_l3ntRoEMqI6K4bC2jL73-Aq54Rd3HrzL-D-2RO08DnVMtJRQfbQ0aouqP-jW27u5J8a5n7JOpvtbfnxy-6QQRPH-Rv92DQMVU52QqcqEIQHQT3m5-5bbN3ht6IJtJkeVIthtKvbfP0kM4r_24ke5qbyKJ0XaDTJVb512h7keq8CD6JSQpKzMxjKah-fKDKt-pu-a-TFSJr2y5jHhPFNetJtybQ3tg3D0loI3RbpsIJM3nAWbT8U5tcWatTLaKviahoHBMb1DCJDBT5h2M4qMxtOLR3pWDTm_q5TtUJMeCnTD-Dhe6j0Dauft5tef5R2stoHK-5-HJOdMtTHq4tehHRwajO9WDTm_D_5-C5EbK5I-tRRX4DtjJOwQ6LjbHKf-pPK5DOmE4QJqJAb3UtjWH5MWTbT3mkjbn5zfn02OP5PqfjdDt4syPRGKxRnWI_jKfA-b4ncjRcTehoM3xI8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hJo:C; BDRCVFR[TWuczE3RSRf]=mk3SLVN4HKm; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1681184674; ab_sr=1.0.1_M2EzM2I5MTUyYWJkNGM3OTYwZmYxNDFmODA1Yzg5MDFlMzc3NmJiZjRkYzQ3ZTQ1NjY2Zjk5N2Y4YjYyZTk5OTEyM2E1NDY5MzFkODM5MDNmY2JjZTIxMjUxNDZiYzI4ZjM1ZGRjNTEzZmQ2ZjIwYmRkYTc1MmUyYWI2NDkyYTgwODIxZmU1MDliMTM3YmQ0YjZlOGQzOWNkZWY2YTAyYw==',
    # 'Host': 'fanyi.baidu.com',
    # 'Origin': 'https://fanyi.baidu.com',
    # 'Referer': 'https://fanyi.baidu.com/',
    # 'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    # 'sec-ch-ua-mobile': '?0',
    # 'sec-ch-ua-platform': 'Windows',
    # 'Sec-Fetch-Dest': 'empty',
    # 'Sec-Fetch-Mode': 'cors',
    # 'Sec-Fetch-Site': 'same-origin',
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    # 'X-Requested-With': 'XMLHttpRequest'
    "Cookie": "BAIDUID=D74FBCCC17F5059BD6537E66151F0551:FG=1; BIDUPSID=ACEB829C291C2FF19CF5888C84F9C1C4; PSTM=1679295369; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; H_PS_PSSID=; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1679295607,1680138269,1680241885; BDSFRCVID=Nj0OJeC627q5i85fakWNbi5TNQ50vY7TH6f3PBvC-x7WClY2gL7PEG0PeM8g0Kub-FL1ogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF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5fuQXbOn2-biW2r-2MbdaDJP_IoG2Mn8M4bb3qOpBtQmJeTxoUJ25DnJhbLGe6-bejJbjaufq-jKaK3tsD8KtTrjDCv55MQcy4LdjG5zXnOU35rh_l3ntRoEMqI6K4bC2jL73-Aq54Rd3HrzL-D-2RO08DnVMtJRQfbQ0aouqP-jW27u5J8a5n7JOpvtbfnxy-6QQRPH-Rv92DQMVU52QqcqEIQHQT3m5-5bbN3ht6IJtJkeVIthtKvbfP0kM4r_24ke5qbyKJ0XaDTJVb512h7keq8CD6JSQpKzMxjKah-fKDKt-pu-a-TFSJr2y5jHhPFNetJtybQ3tg3D0loI3RbpsIJM3nAWbT8U5tcWatTLaKviahoHBMb1DCJDBT5h2M4qMxtOLR3pWDTm_q5TtUJMeCnTD-Dhe6j0Dauft5tef5R2stoHK-5-HJOdMtTHq4tehHRwajO9WDTm_D_5-C5EbK5I-tRRX4DtjJOwQ6LjbHKf-pPK5DOmE4QJqJAb3UtjWH5MWTbT3mkjbn5zfn02OP5PqfjdDt4syPRGKxRnWI_jKfA-b4ncjRcTehoM3xI8LNj405OTbIFO0KJzJCcjqR8Zj5t5jTQP; BA_HECTOR=0g8hak81a00l8h008hak80611i39id61n; ZFY=MRxf1IZDlYbUrVomDdSl:BM0Oa6peQ1vBzbnlZHX8hJo:C; BDRCVFR[TWuczE3RSRf]=mk3SLVN4HKm; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1681184674; ab_sr=1.0.1_MmVjZTRkOTUzMzZlOWYyZGRiYjdjYmYzZWQyZjBlNmFhZTI3YWQyMDAzYzA2ZjU5NDFlOWFhZjY3ZmFkYTk5ZTViZGMxOWFhYmU3MGQzYTY1NDhlNGJhOGZhMzUzOTZiMWE5NmY3YzhlZGQzOTU3MGRmNjIxZTIzMDc0YjU3YTVmZGQyYjNkMzg5MzNjZjU4MjkyNjNjYjU5MDhkNGFhOA=="
}
# 请求参数
data = {
    'from': 'en',
    'to': 'zh',
    'query': 'remain',
    'transtype': 'realtime',
    'simple_means_flag': '3',
    'sign': '747852.1018493',
    'token': '553e29115f1bb891689e177864137aa4',
    'domain': 'common',
}
# post请求参数必须编码
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)  # {"errno":997,"errmsg":"\u672a\u77e5\u9519\u8bef","query":"remain","from":"en","to":"zh","error":997}
# 将字符串转 json
print(json.loads(content))
